﻿(function (ns, $) {
    ZYCLib.RegNameSpace(ns).Handdler = (function () {

        var Params = {
            PageIndex: 1,
            IsMarket: 1,
            SortID: 0,
            MBID: 0,
            PCAID: 0,
            Keyword: "",
            MName: "",
            PCAName: ""
        };

        var initPage = function (paramsJson) {
            Params.PageIndex = paramsJson.PageIndex;
            Params.IsMarket = paramsJson.IsMarket ? 1 : 8;
            Params.SortID = paramsJson.SortID;
            Params.MBID = paramsJson.MBID;
            Params.PCAID = paramsJson.PCAID;
            Params.Keyword = paramsJson.Keyword;
            Params.MName = paramsJson.MName;
            Params.PCAName = paramsJson.PCAName;

            if (Params.MBID > 0) {
                $("#pztag").css("display", "block");
                $("#lblCodex").html(Params.MName);
                $("#lblCodex").attr("codexid", Params.MBID);
            }

            if (Params.PCAID > 0) {
                $("#ystag").css("display", "block");
                $("#lblPlace").html(Params.PCAName);
                $("#lblPlace").attr("placeid", Params.PCAID);
            }

            if (Params.Keyword != null && Params.Keyword.length > 0) {
                $("#keyword").val(Params.Keyword);
                $("#keytag").css("display", "block");
                $("#lblKey").html(Params.Keyword);
            }

            //initLetterSearch();
        };
        $(function () {
            if (Params.IsMarket == 1) {
                $("#drugMarketdiv").removeClass("none");
            } else {
                $("#provincesdiv").removeClass("none");
            }
        });

        //搜索btn
        $(function () {
            $("#sub_search").bind("click", function () {
                Params.Keyword = encodeURI($.trim($("#keyword").val()));
                //if (Params.Keyword != null && Params.Keyword.length == 0) {
                //    ZYCLib.UI.MessageBox.Alert("请输入关键字!");
                //    return;
                //}
                Params.PageIndex = 1;
                freshHerf();
            });
        });

        //搜索类型
        var searchType = {
            PZ: { Name: '品种', DomID: "#pztag" },
            QY: { Name: '区域', DomID: "#ystag" },
            PM: { Name: '品名', DomID: "#pmtag" }
        };

        var callBackClick = function () {
            if (Params.MBID != $("#lblCodex").attr("codexid") || Params.PCAID != $("#lblPlace").attr("placeid")) {
                Params.PageIndex = 1;
            }
            Params.MBID = $("#lblCodex").attr("codexid");
            Params.MName = $("#lblCodex").html();
            Params.PCAID = $("#lblPlace").attr("placeid");
            Params.PCAName = $("#lblPlace").html();
            Params.Keyword = $("#lblKey").html();

            freshHerf();
        };

        var sortClick = function (sortID) {
            Params.SortID = sortID;
            Params.PageIndex = 1;
            freshHerf();
        };

        //URL
        var freshHerf = function () {

            var href = "/jiage/" + Params.IsMarket + "-" + Params.MBID + "-" + Params.PCAID + "-" + Params.PageIndex + ".html";
            var flag = false;
            if (Params.Keyword != "") {
                href += "?keyword=" + Params.Keyword;
                flag = true;
            }

            if (flag) {
                if (Params.SortID != 0) {
                    href += "&sortid=" + Params.SortID;
                }
            } else {
                if (Params.SortID != 0) {
                    href += href.indexOf("?") > 0 ? "&sortid=" + Params.SortID : "?sortid=" + Params.SortID;
                }
            }
            window.location = href;
        };

        return {
            InitPage: initPage,
            SearchType: searchType,
            CallBackClick: callBackClick,
            SortClick: sortClick
        };
    })();
})("Price", jQuery)